package com.beisita.web.controller;


import com.beisita.dao.entity.StuEntity;
import com.beisita.service.StuService;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @Author:roger
 * @Date: 2020/9/26
 * @Time: 14:42
 */

@RestController
@CrossOrigin
@RequestMapping("/stu")
@PropertySource(value = "classpath:my.properties")
@ConfigurationProperties(prefix = "test")
public class StuController {

    @Value("${test.name}")
    private String username;

    @Autowired
    private StuService stuService;

    @RequestMapping("getStu/{id}")
    public StuEntity getStu(@PathVariable long id) {
        System.out.println("999999999999" + username);
        return stuService.queryById(id);
    }

    @RequestMapping("getStu/page")
    public List<StuEntity> getStuByPage(@PathVariable(required = false) Long id) {
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("startIndex", 8991111);
        map.put("pageSize", 10);
        return stuService.queryByPage(map);
    }

    @RequestMapping("getStu/page/opt")
    public List<StuEntity> queryByOptPage(@PathVariable(required = false) Long id) {
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("startIndex", 8991111);
        map.put("pageSize", 10);
        return stuService.queryByOptPage(map);
    }

    @ApiOperation(value = "测试", notes = "测试")
    @ApiImplicitParam(name = "user", value = "用户进行测试", required = true, dataType = "user")
    @RequestMapping("/exce")
    public Object showInfo() {
        String info = "你好";
        int a = 1 / 0;
        return info;
    }


    /**
     * 查找所有
     *
     * @return
     */
    @RequestMapping("/getAll")
    public List<StuEntity> getAll() {
        return stuService.getAll();
    }

    /**
     * 根据id获取用户
     *
     * @return
     */
    @RequestMapping("/getOne")
    public StuEntity getOne(Long id) {
        return stuService.getOne(id);
    }

    /**
     * 新增用户
     *
     * @param stu
     * @return
     */
    @RequestMapping("/insertStu")
    public String insertUser(StuEntity stu) {
        stuService.insertStu(stu);
        return "insert success";
    }

    /**
     * 修改用户
     *
     * @param stu
     * @return
     */
    @RequestMapping("/updateStu")
    public String updateUser(StuEntity stu) {
        stuService.updateStu(stu);
        return "update success";
    }
}
